package com.hsmw.api.controller.biz;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hsmw.api.config.jwt.CurrentHsmwUserUtils;
import com.hsmw.api.mapper.HsmwForcemanItemMapper;
import com.hsmw.api.service.*;
import com.hsmw.common.base.BaseController;
import com.hsmw.common.base.BasePage;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import com.hsmw.common.util.MapUtil;
import com.htn.common.core.utils.StringUtils;
import com.hsmw.domain.*;
import com.hsmw.domain.HsmwConstructionUnit;
import com.hsmw.domain.form.GetObjectByIdForm;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author WD
 */
@Api(tags = "BIZ-施工单位管理")
@RestController
@RequestMapping("/api/biz/constructionUnit")
public class BizConstructionUnitController extends BaseController<HsmwConstructionUnitService, HsmwConstructionUnit> {

	@Autowired
	private HsmwConstructionUnitService hsmwConstructionUnitService;

	@Autowired
	private HsmwItemService hsmwItemService;
	@Autowired
	private CurrentHsmwUserUtils currentHsmwUserUtils;

	@Autowired
	private HsmwForcemanItemMapper hsmwForcemanItemMapper;



	@Autowired
	private HsmwPmUserService hsmwPmUserService;

	@Autowired
	private HsmwConcreteUserService hsmwConcreteUserService;

	@Autowired
	private HsmwDriverUserService hsmwDriverUserService;

	@Autowired
	private HsmwBizUserService hsmwBizUserService;

	@Autowired
	private HsmwHtnUserService hsmwHtnUserService;

	@Autowired
	private HsmwTpspUserService hsmwTpspUserService;

	@Autowired
	private HsmwSaleUserService hsmwSaleUserService;
	/**
	 * 分页
	 *
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "获取施工单位分页列表", notes = "获取施工单位分页列表", produces = "application/json")
	@PostMapping("/getHsmwConstructionUnitPage")
	public DataResponse getHsmwConstructionUnitPage(@RequestBody @Valid BasePage<HsmwConstructionUnit> basePage) {
		IPage page = new Page(basePage.getPageNo(), basePage.getPageSize());
		HsmwConstructionUnit hsmwConstructionUnit = basePage.getRequestData();
		IPage<HsmwConstructionUnit> data = hsmwConstructionUnitService.getHsmwConstructionUnitPage(page, hsmwConstructionUnit);
		return new DataResponse(data);
	}


	/**
	 * 获取施工单位下拉
	 *
	 * @return
	 */
	@ApiOperation(value = "获取施工单位下拉", notes = "获取施工单位下拉", produces = "application/json")
	@PostMapping("/getHsmwConstructionUnitSelect")
	public DataResponse getHsmwConstructionUnitSelect() {
		List<HsmwConstructionUnit> data = hsmwConstructionUnitService.list();
		return new DataResponse(data);
	}

	/**
	 * 根据ID查询数据
	 *
	 * @param form
	 * @return
	 */
	@ApiOperation(value = "根据id查询数据", notes = "根据id查询数据", produces = "application/json")
	@PostMapping("/getHsmwConstructionUnitById")
	public DataResponse getHsmwConstructionUnitById(@RequestBody GetObjectByIdForm form) {
		HsmwConstructionUnit hsmwConstructionUnit = hsmwConstructionUnitService.getHsmwConstructionUnitById(form.getKey());
		return new DataResponse(hsmwConstructionUnit);
	}

	/**
	 * 新增
	 * 修改  必须带ID
	 *
	 * @param hsmwConstructionUnit
	 * @return
	 */
	@ApiOperation(value = "新增/修改施工单位", notes = "新增/修改施工单位", produces = "application/json")
	@PostMapping("/saveOrUpdateHsmwConstructionUnit")
	public DataResponse saveOrUpdateHsmwConstructionUnit(@RequestBody HsmwConstructionUnit hsmwConstructionUnit) {
		hsmwConstructionUnitService.saveOrUpdateHsmwConstructionUnit(hsmwConstructionUnit);
		return new DataResponse(true);
	}


	/**
	 * 工长的分页
	 *
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "获取工长的分页列表", notes = "获取工长的分页列表 type传FORCEMAN", produces = "application/json")
	@PostMapping("/getForceManPage")
	public DataResponse getForceManPage(@RequestBody @Valid BasePage<HsmwHtnUser> basePage) {
		IPage page = new Page(basePage.getPageNo(), basePage.getPageSize());
		HsmwHtnUser hsmwUser = basePage.getRequestData();
		IPage<HsmwHtnUser> data = hsmwHtnUserService.getHsmwUserPage(page, hsmwUser);
		List<HsmwItem> hsmwItemList = hsmwItemService.list(Wrappers.lambdaQuery());
		Map<String, HsmwItem> itemMap = hsmwItemList.stream().collect(Collectors.toMap(HsmwItem::getId, Function.identity()));
		data.getRecords().forEach(hsmwUser1 -> {
			if (itemMap.get(hsmwUser1.getItemId()) != null) {
				hsmwUser1.setItemName(itemMap.get(hsmwUser1.getItemId()).getName());
			}
		});
		return new DataResponse(data);
	}

	/**
	 * 修改工长信息
	 *
	 * @param hsmwUser
	 * @return
	 */
	@ApiOperation(value = "修改工长信息", notes = "修改工长信息 传id", produces = "application/json")
	@PostMapping("/saveForceMan")
	public DataResponse saveForceMan(@RequestBody @Valid HsmwHtnUser hsmwUser) {
		List<HsmwHtnUser> users = hsmwHtnUserService.list(new LambdaQueryWrapper<HsmwHtnUser>()
				.eq(HsmwHtnUser::getUsername,hsmwUser.getUsername())
				.ne(HsmwHtnUser::getId,hsmwUser.getId()));
		if(CollectionUtil.isNotEmpty(users)){
			return new DataResponse(CommonConstants.ResponseStatus.FAIL, "工长电话在已经存在，请重新输入！");
		}
		if(StringUtils.isNotBlank(hsmwUser.getItemId())){
			String[] itemIds =  hsmwUser.getItemId().split(",");
			//首先删除以前的对应关系
			hsmwForcemanItemMapper.delete(new LambdaQueryWrapper<HsmwForcemanItem>()
					.eq(HsmwForcemanItem::getForcemanId,hsmwUser.getId()));
			if(itemIds.length>0){
				for(String itemId :itemIds){
					//保存信息到 项目和工长的关联表中
					HsmwForcemanItem forcemanItem = new HsmwForcemanItem();
					forcemanItem.setItemId(itemId);
					forcemanItem.setForcemanId(hsmwUser.getId());
					hsmwForcemanItemMapper.insert(forcemanItem);
				}
			}
		}
		hsmwUser.setItemId(null);
		hsmwHtnUserService.updateById(hsmwUser);
		return new DataResponse();
	}



	/**
	 * 选择工长的下拉列表
	 *
	 * @param jsonObject
	 * @return
	 */
	@ApiOperation(value = "选择工长的下拉列表", notes = "选择工长的下拉列表 新增传constructionUnitId， 修改时候传：constructionUnitId+itemId", produces = "application/json")
	@PostMapping("/getchooseForceManSelect")
	public DataResponse getchooseForceManSelect(@RequestBody JSONObject jsonObject) {
		List<HsmwHtnUser> data = hsmwHtnUserService.getchooseForceManSelect(jsonObject.getStr("itemId"), jsonObject.getStr("constructionUnitId"));
		return new DataResponse(data);
	}



	/**
	 * 获取项目下人员管理的分页列表
	 *
	 * @param basePage
	 * @return
	 */
	@ApiOperation(value = "获取项目下人员管理的分页列表", notes = "获取项目下人员管理的分页列表", produces = "application/json")
	@PostMapping("/getItemUserPage")
	public DataResponse getItemUserPage(@RequestBody @Valid BasePage<HsmwHtnUser> basePage) {
		IPage page = new Page(basePage.getPageNo(), basePage.getPageSize());
		HsmwHtnUser hsmwUser = basePage.getRequestData();
		IPage<HsmwHtnUser> data = hsmwHtnUserService.getHsmwUserPage(page, hsmwUser);
		return new DataResponse(data);
	}

	/**
	 * 新增人员
	 *
	 * @param hsmwUser
	 * @return
	 */
	@ApiOperation(value = "新增项目管理人员", notes = "新增人员  type传PM ", produces = "application/json")
	@PostMapping("/addPM")
	public DataResponse addPM(@RequestBody HsmwPmUser hsmwUser) {
		DataResponse dataResponse = hsmwPmUserService.saveOrUpdateHsmwUser(hsmwUser);
		return dataResponse;
	}

	/**
	 * 根据地址获取经纬度
	 *
	 * @param form
	 * @return
	 */
	@ApiOperation(value = "根据地址获取经纬度", notes = "根据地址获取经纬度", produces = "application/json")
	@PostMapping("/getLatAndLogByName")
	public DataResponse getLatAndLogByName(@RequestBody GetObjectByIdForm form) {
		return new DataResponse(MapUtil.getLatAndLogByName(form.getKey()));
	}

}
